package com.ftw.commons

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.jakewharton.rxbinding3.view.clicks
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import com.trello.rxlifecycle3.components.support.RxFragment
import com.ftw.commons.R
import com.ftw.commons.network.HttpSubscriber.Companion.OTHER_EXCEPTION
import com.ftw.commons.widgets.toast.ToastUtils
import kotlinx.android.synthetic.main.fragment_base.*
import org.greenrobot.eventbus.EventBus
import java.util.concurrent.TimeUnit


/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/2/13  11:04 AM
 */

abstract class BaseFragment : RxFragment() {

    lateinit var mActivity: RxAppCompatActivity
    lateinit var mContext: Context
    private var isLoaded = false

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
        this.mActivity = activity as RxAppCompatActivity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_base, container, false)
    }


    override fun onResume() {
        super.onResume()
        if (!isLoaded && !isHidden) {
            loadData()
            isLoaded = true
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val rootView = LayoutInflater.from(mContext).inflate(getContentViewId(), null)
        ll_content?.addView(rootView)
        iv_back?.setOnClickListener {
            getParentActivity().finish()
        }
        rootView?.let { initView(it) }
    }

    fun getIvBack(): ImageView {
        return iv_back
    }

    /**
     * 给右上角TextView1添加点击事件
     *
     * @param listener
     */
    fun setTvOtherOnClickListener(listener: View.OnClickListener) {
        tv_other?.visibility = View.VISIBLE
        tv_other?.setOnClickListener(listener)
    }

    /**
     * 给右上角TextView2添加点击事件
     *
     * @param listener
     */
    fun setTv2OtherOnClickListener(listener: View.OnClickListener) {
        tv2_other?.visibility = View.VISIBLE
        tv2_other?.setOnClickListener(listener)
    }

    /**
     * 给右上角ImageView添加点击事件
     *
     * @param listener
     */
    fun setIvOtherOnClickListener(listener: View.OnClickListener) {
        iv_other?.visibility = View.VISIBLE
        iv_other?.setOnClickListener(listener)
    }

    fun getTitleRight(): TextView {
        return tv_title3
    }

    /**
     * 给右上角ImageView2添加点击事件
     *
     * @param listener
     */
    fun setIv2OtherOnClickListener(listener: View.OnClickListener) {
        iv2_other?.visibility = View.VISIBLE
        iv2_other?.setOnClickListener(listener)
    }

    /**
     * 对外提供tv_other给调用者提供设置文本机会
     *
     * @return
     */
    fun setTvOtherText(text: String) {
        tv_other?.visibility = View.VISIBLE
        tv_other?.text = text
    }

    /**
     * 对外提供tv2_other给调用者提供设置文本机会
     *
     * @return
     */
    fun setTv2OtherText(text: String) {
        tv2_other?.visibility = View.VISIBLE
        tv2_other?.text = text
    }

    /**
     * 对外提供Iv_other给调用者提供设置图片机会
     * @return
     */
    fun setIvOtherImage(resourcesId: Int) {
        iv_other?.visibility = View.VISIBLE
        iv_other?.setImageResource(resourcesId)
    }

    /**
     * 对外提供Iv2_other给调用者提供设置图片机会
     * @return
     */
    fun setIv2OtherImage(resourcesId: Int) {
        iv2_other?.visibility = View.VISIBLE
        iv2_other?.setImageResource(resourcesId)
    }

    /**
     * 获取公共标题右上角ImageView
     */
    fun getIvOther(): ImageView {
        return iv_other
    }

    /**
     * 获取公共标题右上角ImageView2
     */
    fun getIv2Other(): ImageView {
        return iv2_other
    }

    /**
     * 获取公共标题TextView
     */
    fun getTvTitle(): TextView {
        return tv_title
    }

    /**
     * 获取公共标题右上角TextView
     */
    fun getTvOther(): TextView {
        return tv_other
    }

    /**
     * 获取公共标题右上角TextView 2
     */
    fun getTv2Other(): TextView {
        return tv2_other
    }

    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(title: String) {
        tv_title?.text = title
    }

    fun setTitleLeft(title: String) {
        tv_title2?.visibility = View.VISIBLE
        tv_title2?.text = title
    }

    fun setTitleRight(title: String, onClickListener: View.OnClickListener?) {
        tv_title3?.visibility = View.VISIBLE
        tv_title3?.text = title
        tv_title3?.setOnClickListener(onClickListener)
    }

    /**
     * 设置右上角textView给右上角TextView添加文本内容，点击事件
     *
     * @param listener
     * @param content
     */
    fun setTvOtherOnClickListener(content: String, listener: View.OnClickListener) {
        tv_other?.visibility = View.VISIBLE
        tv_other?.setOnClickListener(listener)
        tv_other?.text = content
    }

    /**
     * 设置右上角textView2给右上角TextView添加文本内容，点击事件
     *
     * @param listener
     * @param content
     */
    fun setTv2OtherOnClickListener(content: String, listener: View.OnClickListener) {
        tv2_other?.visibility = View.VISIBLE
        tv2_other?.setOnClickListener(listener)
        tv2_other?.text = content
    }

    /**
     * 设置右上角图标，给右上角ImageView添加点击事件
     * @param listener
     * @param drawableId
     */
    fun setIvOtherOnClickListener(drawableId: Int, listener: View.OnClickListener) {
        iv_other?.visibility = View.VISIBLE
        iv_other?.setOnClickListener(listener)
        iv_other?.setImageResource(drawableId)
    }

    /**
     * 设置右上角图标，给右上角ImageView2添加点击事件
     * @param listener
     * @param drawableId
     */
    fun setIv2OtherOnClickListener(drawableId: Int, listener: View.OnClickListener) {
        iv2_other?.visibility = View.VISIBLE
        iv2_other?.setOnClickListener(listener)
        iv2_other?.setImageResource(drawableId)
    }

    /**
     * 显示或者隐藏顶部
     * @param isShow
     */
    fun showOrHideTop(isShow: Boolean = true) {
        rl_top?.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    /**
     * 获取顶部标题布局
     */
    fun getRootTitle(): RelativeLayout {
        return rl_top
    }


    /**
     * 给外部提供是否显示底部线条
     */
    fun setLineVisibility(isVisible: Boolean) {
        when (isVisible) {
            true -> {
                line?.visibility = View.VISIBLE
            }
            else -> {
                line?.visibility = View.GONE
            }
        }
    }

    fun getParentActivity(): BaseActivity {
        return mActivity as BaseActivity
    }

    fun popBackStack() {
        getParentActivity().supportFragmentManager.popBackStack()
    }

    open fun onError(code: Int, msg: String) {
        when (code) {
            OTHER_EXCEPTION -> {
                ToastUtils.showError(mContext, msg)
            }
            else -> {
                empty_view?.setContent(getString(R.string.load_fail))
                empty_view?.visibility = View.VISIBLE
                ll_content?.visibility = View.GONE
                empty_view?.setImg(R.drawable.icon_load_failure)
                empty_view.clicks()
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe {
                        loadData()
                    }
            }
        }
    }


    /**
     * 初始化View
     */
    abstract fun initView(view: View)

    /**
     * 数据加载
     */
    abstract fun loadData()

    /**
     * 子类Fragment布局Id,是否启用内容刷新，不请用的情况下和默认的
     */
    abstract fun getContentViewId(): Int


}